package com.example.ymm.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableOpenApi
public class SwaggerConfig {


    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    @Bean
    public Docket docket(Environment environment){
        //开发和生产环境
        Profiles profiles=Profiles.of("dev","prod-new");
        //通过environment.acceptsProfiles()判断是否处在自己设定的环境当中
        boolean swaggerEnabled=environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                //多人开发时分组
               // .groupName("liu")
                //是否启用Swagger
                .enable(swaggerEnabled)
                /* 设置安全模式，swagger可以设置访问token */
                .securityContexts(securityContexts())
                .securitySchemes(securitySchemes())
                .select()
                //基于包进行扫描
              //  RequestHandlerSelectors.basePackage()//基于包进行扫描
                //RequestHandlerSelectors.any()//扫描全部
                //RequestHandlerSelectors.none()//不扫描
                .apis(RequestHandlerSelectors.basePackage("com.example.ymm.modules1.controller"))
                //paths()过滤什么路径
//                .paths(PathSelectors.ant("/controller/**"))

                .build()
                ;
    }



    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> objects = new ArrayList<>();
        objects.add(new ApiKey(tokenHeader, tokenHeader, "header"));
        return objects;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> objects = new ArrayList<>();
        SecurityContext build = SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("^(?!auth).*$")).build();
        objects.add(build);
        return objects;
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        SecurityReference authorization = new SecurityReference(tokenHeader, authorizationScopes);
        List<SecurityReference> arrayList = new ArrayList();
        arrayList.add(authorization);
        return arrayList;
    }


    private ApiInfo apiInfo(){
        //作者信息
        Contact contact=new Contact("刘庆峰","https://127.0.0.1/","1055162826@qq.com");
        return  new ApiInfo("恒丽",
                "恒丽读书",
                "v1.0",
                "",
                contact,
                "https://127.0.0.1",
                "https://127.0.0.1",
                new ArrayList());
    }



}
